Skip to content

add real time face blurring script #411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

ChrisEssomba
Copy link

@ChrisEssomba ChrisEssomba commented Mar 15, 2025

PR Title

Add Real-Time Face Blurring Tool with OpenCV

Summary

summary...
Add the folder containing the script and add the script's information in the Readme file of the project

Description

This PR introduces a real-time face blurring script capable of anonymizing faces in:

-Webcam feeds (live processing)

-Video files (MP4 format)

-Static images (JPG/PNG)

Checks

in the repository

  • Made no changes that degrades the functioning of the repository
  • Gave each commit a better title (unlike updated README.md)

in the PR

  • Followed the format of the pull_request_template
  • Made the Pull Request in a small level (for the creator's wellfare)
  • Tested the changes you made

Thank You,

Chris Essomba

Copy link
Owner

@DhanushNehru DhanushNehru left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the main README.md list of projects section

@ChrisEssomba
Copy link
Author

Hey, I updated the main README.md, let me know if there's anything else to do. Thanks

@DhanushNehru DhanushNehru requested a review from Copilot July 20, 2025 12:16
Copilot

This comment was marked as outdated.

@DhanushNehru DhanushNehru requested a review from Copilot July 21, 2025 10:55
Copilot

This comment was marked as outdated.

@DhanushNehru DhanushNehru requested a review from Copilot July 21, 2025 15:13
Copilot

This comment was marked as outdated.

@DhanushNehru DhanushNehru requested a review from Copilot July 21, 2025 15:23
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a real-time face blurring tool that uses OpenCV and deep learning to anonymize faces in various media formats. The implementation provides functionality for processing webcam feeds, video files, and static images with adjustable blur strength and confidence thresholds.

  • Adds a complete face blurring application with DNN-based face detection
  • Provides support for webcam, video, and image processing workflows
  • Updates the main README with proper formatting fixes and new script entry

Reviewed Changes

Copilot reviewed 4 out of 6 changed files in this pull request and generated 8 comments.

File Description
Real-Time-Face-Blurring-Tool/readme Documentation for the face blurring tool including features, requirements, and installation instructions
Real-Time-Face-Blurring-Tool/protocol/deploy.prototxt.txt Caffe model architecture definition file for the face detection neural network
Real-Time-Face-Blurring-Tool/main.py Main Python implementation containing face detection, blurring logic, and media processing functions
README.md Updates to the main project README with formatting corrections and the new script entry
Comments suppressed due to low confidence (1)

Real-Time-Face-Blurring-Tool/readme:1

  • The filename 'readme' should follow standard conventions and be named 'README.md' for better visibility and markdown rendering.
# Face Blurring Application


# Extract and blur face ROI
face_roi = image[startY:endY, startX:endX]
blurred_face = cv2.GaussianBlur(face_roi, (blur_strength, blur_strength), 0)
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code doesn't validate that blur_strength is odd, which is required for cv2.GaussianBlur. If an even number is passed, OpenCV will raise an error.

Copilot uses AI. Check for mistakes.

Comment on lines +78 to +81
# Extract and blur face ROI
face_roi = image[startY:endY, startX:endX]
blurred_face = cv2.GaussianBlur(face_roi, (blur_strength, blur_strength), 0)
image[startY:endY, startX:endX] = blurred_face
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no validation that the face ROI has non-zero dimensions. If startY >= endY or startX >= endX, this will create an empty array that could cause issues with GaussianBlur.

Suggested change
# Extract and blur face ROI
face_roi = image[startY:endY, startX:endX]
blurred_face = cv2.GaussianBlur(face_roi, (blur_strength, blur_strength), 0)
image[startY:endY, startX:endX] = blurred_face
# Validate ROI dimensions
if endY > startY and endX > startX:
# Extract and blur face ROI
face_roi = image[startY:endY, startX:endX]
blurred_face = cv2.GaussianBlur(face_roi, (blur_strength, blur_strength), 0)
image[startY:endY, startX:endX] = blurred_face

Copilot uses AI. Check for mistakes.

"""

# Load and process the image
image = cv2.imread(image_path)
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function doesn't check if cv2.imread successfully loaded the image. If the file doesn't exist or is corrupted, image will be None, causing a crash in blur_faces().

Suggested change
image = cv2.imread(image_path)
image = cv2.imread(image_path)
if image is None:
print(f"Error: Unable to load image from path '{image_path}'. Please check the file path or file integrity.")
return

Copilot uses AI. Check for mistakes.

# Ensure the output file has a valid extension
output_path = os.path.join(output_folder, os.path.splitext(name)[0] + "_blurred.mp4")

video = cv2.VideoCapture(video_path)
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function doesn't verify that the video capture was successfully opened. If the video file doesn't exist or is corrupted, this could lead to unexpected behavior.

Suggested change
video = cv2.VideoCapture(video_path)
video = cv2.VideoCapture(video_path)
if not video.isOpened():
print(f"Error: Unable to open video file {video_path}. Please check the file path or format.")
return

Copilot uses AI. Check for mistakes.

and allows stopping the recording by pressing 'q'.
"""

video = cv2.VideoCapture(0) # Open webcam
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function doesn't check if the webcam was successfully opened. If no camera is available, this could cause issues in the processing loop.

Suggested change
video = cv2.VideoCapture(0) # Open webcam
video = cv2.VideoCapture(0) # Open webcam
if not video.isOpened():
print("Error: Unable to access the webcam. Please ensure it is connected and not in use.")
return

Copilot uses AI. Check for mistakes.

def load_face_detection_model():
"""Loads the pre-trained face detection model."""
prototxt_path = os.path.join(BASE_DIR, "protocol", "deploy.prototxt.txt")
model_path = os.path.join(BASE_DIR, "model", "res10_300x300_ssd_iter_140000_fp16.caffemodel")
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function doesn't validate that the model files exist before attempting to load them. If the files are missing, this will cause a runtime error.

Suggested change
model_path = os.path.join(BASE_DIR, "model", "res10_300x300_ssd_iter_140000_fp16.caffemodel")
model_path = os.path.join(BASE_DIR, "model", "res10_300x300_ssd_iter_140000_fp16.caffemodel")
# Check if the model files exist
if not os.path.exists(prototxt_path):
raise FileNotFoundError(f"Prototxt file not found: {prototxt_path}")
if not os.path.exists(model_path):
raise FileNotFoundError(f"Model file not found: {model_path}")

Copilot uses AI. Check for mistakes.

out = cv2.VideoWriter(output_path, fourcc, fps, res)
return out

def blur_faces(image, confidence_threshold=0.5, blur_strength=61):
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic number 61 for blur_strength should be defined as a named constant to improve code maintainability and make it easier to adjust.

Suggested change
def blur_faces(image, confidence_threshold=0.5, blur_strength=61):
def blur_faces(image, confidence_threshold=0.5, blur_strength=DEFAULT_BLUR_STRENGTH):

Copilot uses AI. Check for mistakes.

Comment on lines +56 to +59
cv2.resize(image, (300, 300)),
1.0,
(300, 300),
(104.0, 177.0, 123.0)
Copy link
Preview

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic numbers (300, 300) and (104.0, 177.0, 123.0) should be defined as named constants to improve code readability and maintainability.

Suggested change
cv2.resize(image, (300, 300)),
1.0,
(300, 300),
(104.0, 177.0, 123.0)
cv2.resize(image, INPUT_SIZE),
1.0,
INPUT_SIZE,
MEAN_VALUES

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants